<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetData, PatchModifyType } from '@/api/modules/skincheckTicket'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('skincheckTicketList').currentPage

const loading = ref(true)
const total = ref(0)

const SkinKeyList = [{
  key: 'DarkEyeCircle',
  label: '黑眼圈',
},
{
  key: 'Water',
  label: '水分',
},
{
  key: 'SkinColor',
  label: '肤色',
},
{
  key: 'BlackHeads',
  label: '黑头',
},
{
  key: 'Acne',
  label: '痘痘',
},
{
  key: 'Pore',
  label: '毛孔',
},
{
  key: 'Splash',
  label: '色斑',
},
{
  key: 'Wrinkle',
  label: '皱纹',
}]

// 查询表单
const findFrom = reactive({
  ...(cacheData.GetCacheData('skincheckTicketList').fromData),
})

const dataList: any = reactive({
  list: [],
})

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.TicketId && { TicketId: findFrom.TicketId }),
    ...(findFrom.SkinKey !== '~' && { SkinKey: findFrom.SkinKey }),
    ...(findFrom.Status !== '~' && { Status: findFrom.Status }),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('skincheckTicketList').currentSize)
  cacheData.SetCacheData('skincheckTicketList', PageNum, cacheData.GetCacheData('skincheckTicketList').currentSize, findFrom)
}

// 修改状态
function ModifyType(params: any) {
  PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      GetDataList(cacheData.GetCacheData('skincheckTicketList').currentPage, cacheData.GetCacheData('skincheckTicketList').currentSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}
// 添加
function AddData() {
  router.push({
    name: 'skincheckTicketTicketDetail',
  })
}
// 编辑
function EditData(id: number) {
  router.push({
    name: 'skincheckTicketTicketDetail',
    query: {
      id,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('skincheckTicketList', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('skincheckTicketList').currentSize)
  cacheData.SetCacheData('skincheckTicketList', val, cacheData.GetCacheData('skincheckTicketList').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('skincheckTicketList').currentPage || 1
})

onMounted(async () => {
  try {
    GetDataList(cacheData.GetCacheData('skincheckTicketList').currentPage, cacheData.GetCacheData('skincheckTicketList').currentSize)
  }
  catch (error) {
    console.error(error)
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="券ID">
                <el-input v-model="findFrom.TicketId" placeholder="请输入券ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="测肤类目">
                <el-select v-model="findFrom.SkinKey" placeholder="请选择测肤类目">
                  <el-option label="全部" value="~" />
                  <el-option
                    v-for="item in SkinKeyList"
                    :key="item.key"
                    :label="item.label"
                    :value="item.key"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="findFrom.Status" placeholder="请选择状态">
                  <el-option label="全部" value="~" />
                  <el-option label="启用" :value="1" />
                  <el-option label="停用" :value="-1" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="ticketId" label="券ID" width="100" />
        <el-table-column align="center" prop="picUrl" label="封面图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.couponInfo.picUrl" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" label="券信息" width="260">
          <template #default="scope">
            <!-- {{ scope.row.prizeType === 1 ? scope.row.couponInfo.id : scope.row.productInfo.productId }} -->
            <div class="user-name">
              <div>
                <p style="margin: 0;">
                  {{ scope.row.couponInfo?.id }}- {{ scope.row.couponInfo?.name }}
                </p>
                <span>
                  券类型：
                  <el-tag>
                    {{ scope.row.couponInfo?.type === 1 ? '赠品券' : scope.row.couponInfo?.type === 2 ? '奖品券' : scope.row.couponInfo?.type === 3 ? '满减券' : '未定义' }}
                  </el-tag>
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="couponInfo.productCode" label="商品编码" />
        <el-table-column align="center" width="150" prop="skinKey" label="测肤类目">
          <template #default="scope">
            {{
              SkinKeyList.find((item: any) => item.key === scope.row.skinKey)?.label || '未定义'
            }}
          </template>
        </el-table-column>
        <el-table-column header-align="center" width="480" prop="ticketDesp" label="券描述" />
        <el-table-column header-align="center" width="240" prop="linkUrl" label="跳转路径" />
        <el-table-column align="center" width="240" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="240" prop="updateTime" label="修改时间" />
        <el-table-column align="center" label="状态" fixed="right" width="180">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="-1"
              @click="ModifyType({ id: scope.row.id, value: scope.row.status })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('skincheckTicketList').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
